package com.example.controller;

import com.example.entity.Support;
import com.example.repository.SupportRepository;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/supports")
public class SupportController {

    private final SupportRepository repository;

    public SupportController(SupportRepository repository) {
        this.repository = repository;
    }

    @GetMapping
    public List<Support> getAll() {
        return repository.findAll();
    }

    @PostMapping
    public Support create(@RequestBody Support support) {
        System.out.println("id"+support.getRequestId());
        System.out.println("标题："+support.getTitle());
        System.out.println("用户名："+support.getUserName());
        return repository.save(support);
    }

    @PutMapping("/{id}")
    public Support update(@PathVariable String id, @RequestBody Support support) {
        support.setRequestId(id);
        return repository.save(support);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable String id) {
        repository.deleteById(id);
    }

    @GetMapping("/test")
    public String testDatabase() {
        return "Database connection is working!";
    }
}
